<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('开关')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-MerchantMethod-status">
			<div class="form-group">
				<label class="col-sm-3 control-label">选择渠道：</label>
				<div class="col-sm-8">
					<select class="form-control" id="channelId" name="channelId">
						<option value="" selected>----请选择----</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">选择商户：</label>
				<div class="col-sm-8">
					<select class="form-control" id="merchantId" name="merchantId">
						<option value="" selected>----请选择----</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">渠道支付类型：</label>
				<div class="col-sm-8">
					<select class="form-control" id="channelMethodId" name="channelMethodId">
						<option value="" selected>----请选择----</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">渠道-支付方法费率：</label>
				<div class="col-sm-8">
					<input id="ChannelMethodPayRate" name="ChannelMethodPayRate" class="form-control" type="text" readonly>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">商户-支付方法费率：</label>
				<div class="col-sm-8">
					<input id="payRate" name="payRate" class="form-control" type="text" placeholder="0.0001" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
					<div class="onoffswitch">
						<input type="checkbox" th:checked="false" class="onoffswitch-checkbox" id="status" name="status">
						<label class="onoffswitch-label" for="status">
							<span class="onoffswitch-inner"></span>
							<span class="onoffswitch-switch"></span>
						</label>
					</div>
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "merchant/mpMerchantMethod";
        var $form = $("#form-MerchantMethod-status");


        /*选择渠道下拉框*/
        $.post(ctx + "channel/channelInfo/list", {},function (response) {
            if (response.code == 0) {
                var data = response.rows;
                var option = "<option value='-1'>" + "全部" + "</option>";
                for (var i = 0; i < data.length; i++) {
                    option += "<option value='"+ data[i].id +"'>" +data[i].name + "</option>"
                }
                $form.find("select[name='channelId']").html("").append(option);
                // 初始化渠道支付类型
                initChannelMethod(-1);
            }else {
                $.modal.alertError("初始化信息异常");
            }
        });
        /* 渠道下拉框选择事件   更新渠道-支付类型下拉框信息 */
        $form.find("select[name='channelId']").change(function () {
            var channelId = $form.find("select[name='channelId']").val();
            // 更新渠道钱包信息
            initChannelMethod(channelId);
        });





        /*渠道-支付类型下拉框*/
        function initChannelMethod(channelId){
            if (channelId != -1) {
                $.post(ctx + "channel/method/list", {'channelId':channelId},function (response) {
                    var data = response.rows;
                    var option = "<option value='-1'>" + "全部" + "</option>";
                    for (var i = 0; i < data.length; i++) {
                        option += "<option value='"+ data[i].id +"'>" + data[i].payKindName + "</option>"
                    }
                    $form.find("select[name='channelMethodId']").html("").append(option);
                    // 初始化渠道-支付方法 信息
                    initChannelMethodInfo(-1);
                });
			} else {
                var option = "<option value='-1'>" + "全部" + "</option>";
                $form.find("select[name='channelMethodId']").html("").append(option);
                // 初始化渠道-支付方法 信息
                initChannelMethodInfo(-1);
			}

        }
        /* 渠道-支付类型下拉框事件   更新渠道-支付方法 信息信息 */
        $form.find("select[name='channelMethodId']").change(function () {
            var channelIdMethodId =  $form.find("select[name='channelMethodId']").val();
            // 更新渠道钱包信息
            initChannelMethodInfo(channelIdMethodId);
        });



        /*渠道-支付方法 信息*/
        function initChannelMethodInfo(channelIdMethodId){
            if (channelIdMethodId != -1) {
                $.post(ctx + "channel/method/list", {'id':channelIdMethodId},function (response) {
                    var data = response.rows;
                    var channelMethod = data[0];
                    // 渠道-支付方法费率
                    $form.find("input[name='ChannelMethodPayRate']").val(channelMethod.payRate.toFixed(4));
                });
            }else {
                var channelId = $form.find("select[name='channelId']").val();
                $.post(ctx + "channel/method/maxPayRate", {"channelId":channelId},function (response) {
                    $form.find("input[name='ChannelMethodPayRate']").val(response.toFixed(4));
                });
            }
        }


        /*选择商户下拉框 */
        $.post(ctx + "merchant/merchantInfo/list", {},function (response) {
            var data = response.rows;
            var option = "<option value='-1'>" + "全部" + "</option>";
            for (var i = 0; i < data.length; i++) {
                option += "<option value='"+ data[i].id +"'>" + data[i].name + "</option>"
            }
            $form.find("select[name='merchantId']").html("").append(option);
        });



        $.validator.addMethod("regex2",function (value, element, params) {
            var exp = new RegExp(params);
            return exp.test(value);
        },"小于1(可保留四位小数)");
        $.validator.addMethod("number",function (value, element, params) {
            var exp_email = new RegExp(params);
            return exp_email.test(value);
        },"请输入正整数");
		/* 表单校验 */
        $form.validate({
			rules:{
                payRate:{
                    regex2:"^[0]+(.[0-9]{4}|.[0-9]{3}|.[0-9]{2}|.[0-9]{1})?$"
				}
			},
			focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
				var data = $form.serializeArray();
				var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
				if (status != 0){
					data.push({"name": "status", "value": status});
				} else {
					data[5].value = status;
				}
				$.operate.save(prefix + "/methodSetting", data);
	        }
	    }

	</script>
</body>
</html>
